﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IWeReplyService : IAppService
    {
        
        void Add(AddWeReplyInput input);
        void Update(UpdateWeReplyInput input);
        void Delete(string id );

        WeReply GetModel(string Id);

        WeReply GetModelByKeyWord(string key);



        PagedData<WeReply> GetPageData(Pagination page, string keyword);
    }

    public class WeReplyService : AppServiceBase<WeReply>, IWeReplyService
    {
        public WeReplyService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        

        

        public void Add(AddWeReplyInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateWeReplyInput input)
        {
            this.UpdateFromDto(input);
        }

        public WeReply GetModel(string Id)
        {
            return  this.Query.Where(a => a.Id == Id).FirstOrDefault();
        }


        public WeReply GetModelByKeyWord(string key)
        {
            return this.Query.Where(a => a.Title.Contains(key)).FirstOrDefault();
        }


        public void Delete(string id)
        {
            this.DbContext.Delete<WeReply>(a => a.Id == id);
        }


        public PagedData<WeReply> GetPageData(Pagination page,string keyword="")
        {
            var q = this.DbContext.Query<WeReply>();
            q = q.WhereIfNotNullOrEmpty(keyword, a => a.Title.Contains(keyword) );
           
            PagedData<WeReply> pagedData = q.TakePageData(page);
            return pagedData;
        }

 


    }

}
